package com.ruoyi.worklog.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.worklog.domain.WlProject;
import com.ruoyi.worklog.domain.WlStaffProject;
import com.ruoyi.worklog.mapper.WlProjectMapper;
import com.ruoyi.worklog.mapper.WlStaffProjectMapper;
import com.ruoyi.worklog.mapper.WlWorkLogProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.ruoyi.common.constant.WorklogConstants.Leader;

/**
 * @Author cw
 * @Date 2022/7/22 - 07 - 22
 * @Description com.ruoyi.worklog.service
 */
@Service
public class ProjectServiceImpl implements  ProjectService{

    @Autowired
    private WlProjectMapper wlProjectMapper;
    @Autowired
    private WlStaffProjectMapper wlStaffProjectMapper;

    @Autowired
    private WlWorkLogProjectMapper wlWorkLogProjectMapper;
    @Override
    public WlProject selectWlProjectById(Long id) {
        return wlProjectMapper.selectWlProjectById(id);
    }


    @Override
    public List<WlProject> selectWlProjectList(WlProject wlProject) {
        return wlProjectMapper.selectWlProjectList(wlProject);
    }
    public List<WlProject> selectWlProjectListByIds(List<Integer> ids){
        return wlProjectMapper.selectWlProjectListByIds(ids);
    }
    @Override
    public List<WlProject> selectWlProjectListByLeaderId(Long leader_id) {
        return wlProjectMapper.selectWlProjectListByLeaderId(leader_id);
    }

    //SUPPORTS:如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
    //READ_COMMITTED:该隔离级别表示一个事务只能读取另一个事务已经提交的数据。该级别可以防止脏

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public int insertWlProject(WlProject wlProject) {
        //这里插入项目之后，员工项目表也要插入 ,leader_id 就是当前用户
        try {
            wlProjectMapper.insertWlProject(wlProject);
            wlStaffProjectMapper.insertWlStaffProject(new WlStaffProject(wlProject.getLeader_id(), wlProject.getId().intValue(), Leader));
        }catch (Exception e ){
            throw new RuntimeException("新增失败:"+e.getMessage());
        }
        return 1;
    }

    @Override
    public int updateWlProject(WlProject wlProject) {
        return wlProjectMapper.updateWlProject(wlProject);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public int deleteWlProjectByIds(Long[] ids) {
        //删除 项目，那么 员工项目表 也要对应删除，日报-项目表也要删除
        try{
            wlProjectMapper.deleteWlProjectByIds(ids); //这里删除了项目，项目-员工
            wlWorkLogProjectMapper.deleteWorkLogs2(ids);//删除日报-项目表
        }catch (Exception e){
            throw new RuntimeException(e.getMessage()+"删除失败");
        }
        return 1;
    }

    @Override
    public boolean checkProjectNameUnique(WlProject wlProject) {
        WlProject info = wlProjectMapper.selectWlProjectByProjectName(wlProject.getProject_name());
        return info != null;
    }
}
